package org.syncsoft.overalt.communication.impl;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.syncsoft.overalt.common.util.FileUtils;
import org.syncsoft.overalt.communication.api.IConnectionEntityConverter;
import org.syncsoft.overalt.communication.exception.ConnectionException;

/**
 * converter the HttpEntity  to String
 * @author tptfc
 *
 */
public class StringConnectionEntityConverter implements IConnectionEntityConverter<String> {
	String result;
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.communication.api.IConnectionEntityConverter#process(org.apache.http.HttpEntity)
	 */
	@Override
	public synchronized void process(HttpEntity entity) throws ConnectionException {		
		if (entity != null) {
			try {
				setResult(FileUtils.getContent(entity.getContent()));
			} catch (IllegalStateException e) {
				throw new ConnectionException(e);
			} catch (IOException e) {
				throw new ConnectionException(e);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.communication.api.IConnectionEntityConverter#result()
	 */
	@Override
	public synchronized String result() {
		return getResult();
	}

	private String getResult() {
		return result;
	}

	private void setResult(String result) {
		this.result = result;
	}

}
